package abs.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RankMethods {
    //python执行程序目录
    private static String pythonExePath = GlobalConfig.getPythonExePath();
    //textRank.py文件的目录（必须用绝对路径）
    private static String textRankFilePath = GlobalConfig.getTextRankFilePath();
    //mmr.py文件的目录（必须用绝对路径）
    private static String mmrFilePath = GlobalConfig.getMmrFilePath();
    //mmr中使用的stopword.text文件路径
    private static String stopWordPath = GlobalConfig.getStopWordPath();
    //word2vec.py文件的目录（必须用绝对路径）
    private static String word2vecFilePath = GlobalConfig.getWord2vecPath();

    /**
     * MMR算法
     * @param originText
     * @return
     */
    public static List<String> mmr(String originText) {
        List<String> resultList = new ArrayList<>();
        //命令行参数指令
        String[] cmdArgs = new String[] {pythonExePath , mmrFilePath, originText, stopWordPath};
        try {
            //执行命令行
            Process pr = Runtime.getRuntime().exec(cmdArgs);
            //读取命令行结果
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line;
            while ((line = in.readLine()) != null) {
                if (!line.trim().equals("")) {
                    resultList.add(line);
                }
            }
            in.close();
            int re = pr.waitFor();
            if (re == 1){ //py程序执行失败
                return null;
            }
        } catch (Exception e) {
            return null;
        }
        return resultList;
    }

    /**
     * textRank算法
     * @param originText 需要处理的原文
     * @return
     */
    public static Map<String, List<String>> textRank(String originText) {
        Map<String, List<String>> result;
        String[] cmdArgs = new String[] {pythonExePath , textRankFilePath, originText};
        try {
            Process pr = Runtime.getRuntime().exec(cmdArgs);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line;
            result = new HashMap<>();//结果map
            String listName = "";
            List<String> resultList = new ArrayList<>();
            while ((line = in.readLine()) != null) {
                //py输出“#newList#”时，新建list
                if ("#newList#".equals(line)){
                    resultList = new ArrayList<>();
                    continue;
                }
                //py输出“#listEnd#”时，当前list结束，插入结果
                if ("#listEnd#".equals(line)){
                    result.put(listName, resultList);
                    listName = "";
                    continue;
                }
                if ("".equals(listName)){
                    listName = line;
                    continue;
                }
                resultList.add(line);
            }
            in.close();
            int re = pr.waitFor();
            if (re == 1){ //py程序执行失败
                return null;
            }
        } catch (Exception e){
            return null;
        }
        return result;
    }

    /**
     * word2vec算法
     * @param originText
     * @return
     */
    public static List<String> word2vec(String originText) {
        List<String> resultList = new ArrayList<>();
        String[] cmdArgs = new String[] {pythonExePath , word2vecFilePath, originText};
        try {
            Process pr = Runtime.getRuntime().exec(cmdArgs);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line;
            while ((line = in.readLine()) != null) {
                if (!line.trim().equals("")) {
                    resultList.add(line);
                }
            }
            in.close();
            int re = pr.waitFor();
            if (re == 1){ //py程序执行失败
                return null;
            }
        } catch (Exception e) {
            return null;
        }
        return resultList;
    }
}
